﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZhouYu.DreamTour.Core;

namespace ZhouYu.DreamTour.WinService
{
    public static class DBFactory
    {

        //读取Web配置文件连接字符串
        private static string connString = ConfigurationHelper.GetAppSettingsValue("connString");

        public static MySqlConnection CreateConnection()
        {
            MySqlConnection conn = new MySqlConnection(connString);
            conn.Open();
            return conn;
        }
    } 
}



//        private static IDbConnection _sharedConnection;
//        private static DbProviderFactory _factory = DbProviderFactories.GetFactory("MySql.Data.MySqlClient");

//        private static bool ShouldCloseConnectionAutomatically { get; set; }

//        /// <summary>
//        /// 打开共享链接
//        /// </summary>
//        public static void CreateConnection()
//        {
//            OpenSharedConnectionImplement(false);
//        }
//        private static void OpenSharedConnectionImplement(bool isInternal)
//        {
//            //这里注意数据库链接状态的判断
//            if (_sharedConnection != null && _sharedConnection.State != ConnectionState.Broken && _sharedConnection.State != ConnectionState.Closed)
//                return;
//            ShouldCloseConnectionAutomatically = isInternal;

//            _sharedConnection = _factory.CreateConnection();
//            if (_sharedConnection == null) throw new Exception("Sql connection failed to configure providername");
//            if (_sharedConnection.State == ConnectionState.Broken)
//            {
//                _sharedConnection.Close();
//            }
//            if (_sharedConnection.State == ConnectionState.Closed)
//            {
//                _sharedConnection.Open();
//            }
//        }
//        private static void CloseSharedConnectionInternal()
//        {
//            if (ShouldCloseConnectionAutomatically)
//                CloseSharedConnection();
//        }
//        /// <summary>
//        /// 关闭共享链接
//        /// </summary>
//        public static void CloseSharedConnection()
//        {
//            if (_sharedConnection == null) return;

//            _sharedConnection.Close();
//            _sharedConnection.Dispose();
//            _sharedConnection = null;
//        }

//        public static DbConnection CreateConnection()
//        {
//            // Assume failure.
//            DbConnection connection = null;

//            // Create the DbProviderFactory and DbConnection.
//            if (connString != null)
//            {
//                try
//                {
//                    DbProviderFactory factory =
//                        DbProviderFactories.GetFactory("MySql.Data.MySqlClient");

//                    connection = factory.CreateConnection();
//                    connection.ConnectionString = connString;
//                }
//                catch (Exception ex)
//                {
//                    // Set the connection to null if it was created.
//                    if (connection != null)
//                    {
//                        connection = null;
//                    }
//                    Console.WriteLine(ex.Message);
//                }
//            }
//            // Return the connection.
//            return connection;
//        }
//    }
//}
